package com.ribbon.MyInterceptor;

import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;

import java.io.IOException;
import java.net.URI;

/**
 * 自定义一个负载均衡的拦截器
 * Created by qhe on 2018/8/16.
 */
public class MyLoadBalancerInterceptor implements ClientHttpRequestInterceptor {
    @Override
    public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes, ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
        final URI originalUri = httpRequest.getURI();
        String servicename = originalUri.getHost();
        System.out.println("进入自定义的请求拦截器中："+servicename);
        return clientHttpRequestExecution.execute(httpRequest,bytes);
    }
}
